package com.base.study.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.base.commonlib.base.CBaseMvpFragment;
import com.base.commonlib.base.adapter.BaseAdapter;
import com.base.commonlib.base.adapter.BaseHolder;
import com.base.study.R;
import com.base.study.bean.CurriculumCatalogBean;
import com.base.study.bean.OnlineStudySecondListBean;
import com.base.study.databinding.FragmentOnlineStudySecondBinding;
import com.base.study.presenter.OnlineStudSecondListPresenter;
import com.base.study.view.OnlineStudySecondListView;
import com.base.study.widget.RecyclerViewSpacesItemDecoration;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;

/**
 * 在线学习第二层
 * Created by zw on 2020/2/13.
 */

public class OnlineStudySecondFragment extends CBaseMvpFragment<OnlineStudSecondListPresenter> implements OnlineStudySecondListView{

    private FragmentOnlineStudySecondBinding mBinding;
    private List<OnlineStudySecondListBean> mData = new ArrayList<>();
    private BaseAdapter<OnlineStudySecondListBean> mAdapter;
    private int page = 1;
    private String id;
    private Disposable disposable;


    @Override
    public void init() {
        mBinding = (FragmentOnlineStudySecondBinding) mRootBinding;
        mPresenter = new OnlineStudSecondListPresenter( this,getActivity());
    }

    @Override
    public void start() {
        Bundle bundle =this.getArguments();
        id = bundle.getString("id");
        initRecyClerView();

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        disposable.dispose();
    }

    private void initRecyClerView() {

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        mBinding.rvOnlineList.setLayoutManager(linearLayoutManager);
        mBinding.rvOnlineList.addItemDecoration(new RecyclerViewSpacesItemDecoration
                (10, 0, 10, 10));

        mAdapter = new BaseAdapter<OnlineStudySecondListBean>(mData, mContext) {
            @Override
            public void convert(BaseHolder holder,  OnlineStudySecondListBean bean, boolean isHeader, boolean isBottom) {
                TextView title = holder.getView(R.id.name);
                title.setText(bean.getName());
            }
        };
        mAdapter.putNormalType(R.layout.item_online_study_second);
        mBinding.rvOnlineList.setAdapter(mAdapter);
        mPresenter.getList(id);
    }


    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_online_study_second;
    }

    @Override
    public void returnNoData() {
        if (mAdapter != null) {
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void returnList(List<OnlineStudySecondListBean> bean) {
        mBinding.empty.setVisibility(View.GONE);
        mData.clear();
        mData.addAll(bean);
        if (mAdapter != null) {
            mAdapter.notifyDataSetChanged();
        }

    }

    @Override
    public void returnMoreList(List<OnlineStudySecondListBean> bean) {

    }


    @Override
    public void returnNoMoreData() {
        mBinding.empty.setVisibility(View.GONE);
    }

    @Override
    public void returnError() {
        mBinding.empty.setVisibility(View.GONE);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mPresenter.release();
    }

}
